<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 生成随机字符串
 * @param  [type] $num   [description]
 * @param  [type] $extra [description]
 * @return [type]        [description]
 */
function rand_string($num,$extra = null){
    $flag = true;
    $str = '';

    $chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'];
    if(is_array($extra))
        $chars = array_merge($chars,$extra);

    $num < 10 ? $flag = false : $num = $num -4;
    $length = count($chars) - 1;
    shuffle($chars);
    for($i = 0;$i < $num; $i++){

        $key = mt_rand(0,$length);
        $str .= $chars[$key];

    }

    if(true == $flag){
        $str .= mt_rand(1000,9999);
    }

    return $str;
}

function sql_to_data($sql,$exp=0){
    $md5 = md5($sql);

    if($data = \think\Cache::get($md5)){
        return $data;
    }

    $data = \think\Db::query($sql);
    \think\Cache::set($md5,$data,$exp);
    return $data;
}

/**
 * 直接跳转到指定地址
 * @return [type] [description]
 */
function self_header($url){

    header('Location: '.$url);
    exit;
}

/**
 * 删除字符串中的空格和双字节空格
 * @return [type] [description]
 */
function str_convert($str){
    $str = stripslashes($str);
    return str_replace("　","",preg_replace("/[\\r\\n\s]*/","",$str));
}
